﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;
using System.Threading;
namespace Infrastructure.Logging
{
    internal class DefaultLogger:TextWriter,ILogger
    {
        private ILog logger = null;
        private Encoding encoding = Encoding.UTF8;
        private static ILogger instance = null;
        private string identity = string.Empty;
        public static ILogger Instance
        { 
            get
            {
                if(instance == null) instance = new DefaultLogger();
                return instance;
            }
        }

        public static ILogger CreateInstance() 
        {
            return CreateInstance(string.Empty);
        }

        public static ILogger CreateInstance(string identity)
        {
            if (instance == null) instance = new DefaultLogger(identity);
            else instance.Identity = identity;
            return instance;
        }

        private DefaultLogger() 
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = LogManager.GetLogger("sys-logger");
        }

        private DefaultLogger(string identity):this()
        {
            if(!string.IsNullOrEmpty(identity) && identity.Trim() != string.Empty) Identity = identity;
        }

        public void Write(Level level, object content)
        {
            Exception ex = content as Exception;
            string msg = string.Empty;
            if (ex != null) msg = ex.Message;
            else msg = content.ToString();
            switch (level)
            {
                case Level.Fatal: logger.Fatal(msg, ex); break;
                case Level.Error: logger.Error(msg, ex); break;
                case Level.Warn: logger.Warn(msg, ex); break;
                case Level.Info: logger.Info(msg, ex); break;
                case Level.Debug: logger.Debug(msg, ex); break;
            }
        }

        public override void Write(object content)
        {
            Write(Level.Info,content);
        }

        public void Fatal(object content)
        {
            logger.Fatal(content);
        }
        public void Error(object content)
        {
            logger.Error(content);
        }
        public void Warn(object content)
        {
            logger.Warn(content);
        }
        public void Info(object content)
        {
            logger.Info(content);
        }
        public void Debug(object content)
        {
            logger.Debug(content);
        }

        public override Encoding Encoding
        {
            get { return encoding; }
        }

        public string Identity
        {
            get
            {
                return identity;
            }
            set
            {
                identity = value;
                log4net.MDC.Set("Identity", identity);
            }
        }
    }
}
